package com.wrx.club_management_backend.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.wrx.club_management_backend.dto.UserDTO;
import com.wrx.club_management_backend.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 源数据处理器
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * 插入自动填充
     * @param metaObject 填充对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {

        UserDTO user = UserHolder.getUser();
        Long currentId = user == null || user.getId() == null ? 1L : user.getId();
        log.info("公共字段自动填充中【insert】");
        log.info(metaObject.toString());
        log.info("已从BaseContext中获取到当前的UID，UID = {}",currentId);
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser",currentId);
        metaObject.setValue("createTime",LocalDateTime.now());
        metaObject.setValue("createUser",currentId);
    }

    /**
     * 更新自动填充
     * @param metaObject 填充对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Long currentId = UserHolder.getUser().getId();
        log.info("公共字段自动填充中【update】");
        log.info(metaObject.toString());
        log.info("已从BaseContext中获取当前的UID，UID={}",currentId);
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",currentId);
    }
}
